import { assign } from 'lodash-es'
import pagesJson from '@/pages.json'

const { pages, subPackages, tabBar } = pagesJson

// 将pages.json转换成Map对象,path为key
const pagesMap = new Map()

pages.forEach((page) => {
  pagesMap.set(page.path, page)
})

if (Array.isArray(subPackages) && subPackages.length) {
  subPackages.forEach((el) => {
    const rootPath = el.root
    el.pages.forEach((page) => {
      page.path = `${rootPath}/${page.path}`
      pagesMap.set(page.path, page)
    })
  })
}

if (tabBar) {
  const tabBarList = tabBar.list
  if (Array.isArray(tabBarList)) {
    tabBarList.forEach((el) => {
      if (pagesMap.has(el.pagePath)) {
        const page = pagesMap.get(el.pagePath)
        const meta = page?.meta || {}
        meta.tabBar = true
        page.meta = assign({}, meta)
        pagesMap.set(el.pagePath, page)
      }
    })
  }
}

export { pagesMap }
